package com.app.view.component.room.expressions 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	
	import org.component.container.FlipPage;
	import org.component.events.FlipPageEvent;
	import org.component.events.PageEvent;
	
	import com.app.model.data.ExpressionItemData;
	
	import com.peachcastle.tools.SmartPlayer;
	
	import com.peachcastle.core.Display;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class ExpressionManager extends Display 
	{	
		private var _roomView:MovieClip = null;
		
		private var _flip:FlipPage = null;
		
		private var _expWindow:MovieClip = null;
		private var _exps:Array = [ { name:"Cute", len:27 } ];
		
		private var _smartPlayer:SmartPlayer = null;
		
		public function ExpressionManager(ui:MovieClip = null, roomView:MovieClip = null) 
		{
			_roomView = roomView;
			
			super(ui, true);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_expWindow = assest["expWindow"];
			
			_flip = new FlipPage(assest, ExpressionItem, 6, 5, { name:"item"} );
			
			_smartPlayer = new SmartPlayer();
		}
	
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
			assest.addEventListener(MouseEvent.CLICK, onAssestClick);
			
			_flip.addEventListener(FlipPageEvent.FLIPPAGE_OVER, onFlipOver);
			_flip.addEventListener(FlipPageEvent.FLIPPAGE_OUT, onFlipOut);
			_flip.addEventListener(FlipPageEvent.FLIPPAGE_SELECT, onFilpSelect);
		}
		
		private function onFilpSelect(event:FlipPageEvent):void
		{
			hide();
			
			_show = false;
			var d:ExpressionItemData = ExpressionItemData(event.item.data);
			if (d)
			{
				var evt:PageEvent = new PageEvent(PageEvent.PAGE_SEND_FACE);
				evt.id = (1) << 8 | d.id;
				dispatchEvent(evt);
				evt = null;
			}
		}
		private function onFlipOver(event:FlipPageEvent):void
		{
			var d:ExpressionItemData = ExpressionItemData(event.item.data);
			if (d)
			{
				showExp(true, d.label);
			}else
			{
				showExp(false);
			}
		}
		private function onFlipOut(event:FlipPageEvent):void
		{
			showExp(false);
		}
		private function onAssestClick(event:MouseEvent):void
		{
			event.stopPropagation();
		}
		private function onSelect(index:int = 0):void
		{
			var exp:Array = ExpConfig.getIndexByExp(index);
			var data:Array = [];
			var len:int = _exps[index].len;
			var name:String = _exps[index].name;
			var item:ExpressionItemData = null;
			for (var i:int = 0; i <len; i++)
			{
				item = new ExpressionItemData();
				item.content = ApplicationDomain.currentDomain.getDefinition("expression.button." + name + (i + 1)) as Class;
				item.label = "expression." + name + (i + 1);
				item.id = i+1;
				
				data[data.length] = item;
			}
			
			_flip.update(data);
		}
		
		public function showExp(show:Boolean, label:String = ""):void
		{
			if (_expWindow.numChildren > 1) _expWindow.removeChildAt(1);
				
			if (label != "")
			{
				var _class:Class = ApplicationDomain.currentDomain.getDefinition(label) as Class;
				var _mc:MovieClip = new _class();
				_mc.width = _mc.height = _expWindow.width;
				_mc.x = _mc.y = _mc.width >> 1;
				_expWindow.addChild(_mc);
			}
			
			_expWindow.visible = show;
			
		}
		
		
		public function hide():void
		{
			if (assest.stage) assest.stage.removeEventListener(MouseEvent.CLICK, onAssestStageClick);
			
			super.hideAssest();
			
			_show = false;
		}
		
		public function show():void
		{
			super.showAssest();
			
			_index = 0;
			_show = true;
			
			onSelect();
			
			assest.stage.addEventListener(MouseEvent.CLICK, onAssestStageClick);
		}
		
		private var _index:int = 0;
		private function onAssestStageClick(event:MouseEvent):void
		{
			_index++;
			if (_index > 1)
			{
				hide();
			}
		}
		
		private var _show:Boolean = false;
		public function get isshow():Boolean { return _show; }	
		
		public function playExpression(p:Point, expression:int, mid:int = 0):void
		{
			var id:int = expression & 0xFF;
			//var type:int = expression >> 8;
			var type:int = 0;
			
			Log.debug("playExpression", id, type);

			var _class:Class = getNameByClass("expression." + _exps[type].name + id);
			var mc:MovieClip = new _class();
			if (p)
			{
				mc.x = p.x, mc.y = p.y;
			}
			_roomView.addChild(mc);
			_smartPlayer.play(mc, mid);
		}
	}

}